<template>
  <div>
    <Category :isShowTable="!isShowTable" />
    <el-card style="margin: 10px 0">
      <div v-show="isShowTable === 0">
        <el-button
          :disabled="!categoryStore.c3Id"
          type="primary"
          icon="Plus"
          size="small"
          @click="addSpu"
        >
          添加SPU
        </el-button>
        <!-- table!!! -->
        <el-table
          v-loading="isLoading"
          :data="tableData"
          border
          stripe
          style="margin: 10px 0"
        >
          <el-table-column
            label="序号"
            width="100px"
            load
            align="center"
            type="index"
          ></el-table-column>
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column label="SPU描述" prop="description"></el-table-column>
          <el-table-column label="SKU操作" align="center" width="150px">
            <template #default="{ row }">
              <el-button
                size="small"
                icon="Plus"
                @click="addSku(row)"
              ></el-button>
              <el-button
                @click="showSkuInfo(row)"
                size="small"
                type="info"
                icon="Pointer"
              ></el-button>
            </template>
          </el-table-column>
          <el-table-column label="SPU操作" align="center" width="150px">
            <template #default="{ row }">
              <el-button
                size="small"
                type="primary"
                icon="Edit"
                @click="updateSpu(row)"
              ></el-button>
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="InfoFilled"
                icon-color="#626AEF"
                :title="`确定要删除${row.spuName}?`"
                @confirm="deleteCurSpu(row)"
              >
                <template #reference>
                  <el-button
                    size="small"
                    type="danger"
                    icon="Delete"
                  ></el-button>
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          :disabled="!total"
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[5, 10, 15]"
          :small="true"
          background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="getSpuList"
        />
      </div>
      <!-- 添加/修改spu时展示内容 -->
      <Spu
        ref="spu"
        v-show="isShowTable === 1"
        @changeShowType="changeShowType"
      />
      <!-- 展示sku区域 -->
      <Sku
        ref="sku"
        v-show="isShowTable === 2"
        @changeShowType="changeShowType"
      />
    </el-card>
    <el-dialog v-model="dialogVisitable" title="sku信息展示" align-center>
      <el-table :data="skuInfoData" border>
        <el-table-column
          type="index"
          label="序号"
          width="80px"
          align="center"
        ></el-table-column>
        <el-table-column prop="skuName" label="SKU名称"></el-table-column>
        <el-table-column prop="price" label="SKU价格"></el-table-column>
        <el-table-column prop="weight" label="SKU重量"></el-table-column>
        <el-table-column label="SKU图片">
          <template #default="{ row }">
            <el-image
              :src="row.skuDefaultImg"
              style="width: 100px; height: 100px"
            ></el-image>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisitable = false">取消</el-button>
          <el-button type="primary" @click="dialogVisitable = false">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
// 引用type
import {
  currentSpuResponseData,
  spuRecords,
  spuData,
  SkuData,
} from '@/api/product/spu/type'
import { ref, watch, onBeforeUnmount, onMounted } from 'vue'
import { reqCurrentSpuList, skuInfo, deleteCurrentSpu } from '@/api/product/spu'
import Sku from './skuForm/index.vue'
import Spu from './spuForm/index.vue'
// 引入category仓库
import useCategoryStore from '@/store/modules/category'
import { ElMessage } from 'element-plus'
const categoryStore = useCategoryStore()

const tableData = ref<spuRecords>([])
const isShowTable = ref(0)
// table loading 状态
const isLoading = ref(false)
// 分页器
const currentPage = ref<number>(1)
const pageSize = ref<number>(5)
const total = ref(0)
// spu的vc
const spu = ref()
const sku = ref()
watch(
  () => categoryStore.c3Id,
  (cur, pre) => {
    if (cur && cur !== pre) {
      getSpuList()
    }
  },
)
onMounted(() => {
  getSpuList()
})
onBeforeUnmount(() => {
  //  初始化仓库响应式数据
  categoryStore.$reset()
})
// 获取spu列表
const getSpuList = async (pager = 1) => {
  currentPage.value = pager
  isLoading.value = true
  const res: currentSpuResponseData = await reqCurrentSpuList(
    currentPage.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (res.code === 200) {
    tableData.value = res.data.records
    total.value = res.data.total
  }
  setTimeout(() => {
    isLoading.value = false
  }, 300)
}
// 分页器切换显示条数
const handleSizeChange = (value: number) => {
  pageSize.value = value
  getSpuList()
}
// 当前spu添加sku
const addSku = (row: spuData) => {
  sku.value.initData(row)
  isShowTable.value = 2
}
// 更新spu
const updateSpu = (row: spuData) => {
  spu.value.initUpdateSpu(row)
  setTimeout(() => {
    isShowTable.value = 1
  }, 100)
}
const addSpu = () => {
  spu.value.initAddSpu(categoryStore.c3Id)
  isShowTable.value = 1
}
// spu子组件点击取消
const changeShowType = (obj: any) => {
  isShowTable.value = obj.flag
  obj.params === 'update' ? getSpuList(currentPage.value) : getSpuList()
}

const skuInfoData = ref<SkuData[]>([])
const dialogVisitable = ref(false)
// 展示skuInfo
const showSkuInfo = async (row: spuData) => {
  const res = await skuInfo(row.id as number)
  if (res.code === 200) {
    skuInfoData.value = res.data
    dialogVisitable.value = true
  }
}
// 删除当前spu
const deleteCurSpu = async (row: spuData) => {
  const res = await deleteCurrentSpu(row.id as number)
  if (res.code === 200) {
    ElMessage.success('删除成功')
    getSpuList(
      tableData.value.length > 1 ? currentPage.value : currentPage.value - 1,
    )
  }
}
</script>

<style lang="scss" scoped></style>
